package com.ruoyi.web.pojo.vo.devicedata;

import com.ruoyi.web.pojo.dto.devicedata.DeviceFactorCountData;
import com.ruoyi.web.utils.influxdb.DeviceData;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class DeviceFactorDataVO {

    @ApiModelProperty(value = "设备编号")
    private String deviceId;

    @ApiModelProperty(value = "设备名称")
    private String deviceName;

    @ApiModelProperty(value = "因子名称")
    private String factorName;

    @ApiModelProperty(value = "因子key")
    private String factorKey;

    @ApiModelProperty(value = "因子单位")
    private String factorUnit;

//    @ApiModelProperty(value = "因子数据类型")
//    private String factorDataType;

    @ApiModelProperty(value = "因子数据")
    private List<DeviceFactorCountData> countData;

    public void buildCountData(List<DeviceData> countData) {
        List<DeviceFactorCountData> data = countData.stream().map(DeviceFactorCountData::new).collect(Collectors.toList());
        this.countData = data;
    }

    public void buildCountData(List<DeviceData> countData,String factor) {
        Map<String, List<DeviceFactorCountData>> map = countData.stream().map(da -> new DeviceFactorCountData(da, factor))
                .collect(Collectors.groupingBy(da -> da.getDate()));

        List<DeviceFactorCountData> factorDataList = new ArrayList<>();
        for (String key : map.keySet()) {
            List<DeviceFactorCountData> list = map.get(key);

            DeviceFactorCountData factorCountData = list.get(0);
            double sum = list.stream()
                    .filter(data -> data.getDValue() != null)
                    .mapToDouble(DeviceFactorCountData::getDValue).sum();
            factorCountData.setDValue(sum);

            factorDataList.add(factorCountData);
        }

        this.countData = factorDataList;
    }
}
